package com.deng.permission.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.deng.common.SQLHelper;
import com.deng.permission.vo.LogVo;
import com.deng.permission.dao.ILog;
import com.deng.util.DateTools;

public class LogImpl extends SQLHelper implements ILog {

	public void addLog(String user_id, String user_name, String module,
			String operation) {
		try
		{
						
			Connection conn = getConnect();
			StringBuffer sb = new StringBuffer("insert into t_sys_log (operation_time,module,operation,user_id,user_name)" +
					" values(");
			sb.append("'"+DateTools.dtsWithTime(new Date())+"','"+module+"','"+operation+"','"+user_id+"','"+user_name+"')");
			updateData(conn, sb.toString());
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public List<Object> getLogByConditions(String user_name,String user_id, String module,
			String operation, String fromTime, String toTime,int page,int pos) {
		// TODO Auto-generated method stub
		List<Object> rtL = new ArrayList();
		try	{
			
			StringBuffer sb = new StringBuffer(" from t_sys_log");
			StringBuffer where = new StringBuffer("");
			
			if(user_name != null && !"".equals(user_name))
			{
				where.append(" user_name like '%"+user_name+"%' and");
			}
			if(user_id != null && !"".equals(user_id))
			{
				where.append(" user_id = '"+user_id+"' and");
			}
			if(module != null && !"".equals(module))
			{
				where.append(" module like '%"+module+"%' and");
			}
			if(operation != null && !"".equals(operation))
			{
				where.append(" operation = '"+operation+"' and");
			}
			if(fromTime != null && !"".equals(fromTime))
			{
				where.append(" operation_time >= '"+fromTime+"' and");
			}
			if(toTime != null && !"".equals(toTime))
			{
				where.append(" operation_time < '"+toTime+"' and");
			}
			String tmpSB = "";
			if(where.length() > 0)
			{
				tmpSB = sb.toString()+" where "+where.substring(0, where.length()-3);
				sb = sb.append(" where ").append(where.substring(0, where.length()-3)).append("order by id");
			}
			
			int rows = 26;
			
			String x = "select top "+rows+" *  "+ tmpSB.toString()+" and id > " +
					"	(select case when max(id) is null then 0 else max(id) end" +
					"	from  (select top "+(page-1+pos)*rows+" * "+sb.toString()+") as k)";
//			System.out.println(sb.toString());
//			System.out.println(tmpSB.toString());
//			System.out.println(x);
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,x);
			List<LogVo> l = new ArrayList<LogVo>();
			LogVo vo ;//= new LogVo();
			while(rs.next())
			{
				vo = new LogVo();
				vo.id = rs.getInt("id");
				vo.module = rs.getString("module");
				vo.operation = rs.getString("operation");
				vo.operation_time = DateTools.dtsWithTime(rs.getTimestamp("operation_time"));
				vo.user_id = rs.getString("user_id");
				vo.user_name = rs.getString("user_name");
//				System.out.println(vo.operation_time);
				l.add(vo);
			}
			
			ResultSet rs2 = queryData(conn,"select count(id) "+tmpSB);
			List<Integer> l2 = new ArrayList() ;
			while(rs2.next()){
				l2.add(rs2.getInt(1));
			}
			
			int max = (int)Math.ceil((double)l2.get(0)/(double)rows);
			
			rtL.add(page+pos);
			rtL.add(max);
			rtL.add(l);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return rtL;
	}

}
